from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
from testcases.api.login_test import TestCaseLogin


class TestCaseOrder(HttpRunner):
    config = Config("登录新增代理商").base_url("${read_config()}")

    teststeps = [
        Step(
            RunTestCase("步骤1-登录")
            .call(TestCaseLogin)
            .export("token", "Uid")  # 确保导出 token
        ),
        Step(
            RunRequest("步骤2-新增代理商地区")
            .post("/api/area/add")
            .with_headers(
                **{"User-Agent": "python-requests/2.32.4", "token": "${clean_token($token)}", "Uid": "${int_str($Uid)}", "Content-Type": "application/json"}
            )
            # .with_headers(token="${clean_token($token)}",Uid="$Uid")  # 正确传递 headers,Uid
            .with_json({"atype": 2, "name": "${gen_random_name()}", "sort": "${gen_random_sort()}"})
            # .with_data({"atype": 2, "name": "${gen_random_name()}", "sort": "${gen_random_sort()}"})
            .extract()
            .with_jmespath("request.headers", "req_headers")
            .with_jmespath("request.body", "req_body")
            .validate()
            .assert_equal("status_code", 200)
        ),
        Step(
            RunRequest("步骤3-新增代理商")
            .post("/api/user/register")
            # .with_headers(
            #     **{"User-Agent": "python-requests/2.32.4", "token": "${clean_token($token)}", "Uid": "${int_str($Uid)}", "Content-Type": "application/json"}
            # )
            # .with_headers(token="${clean_token($token)}",Uid="$Uid")  # 正确传递 headers,Uid
            .with_json({"username":"${gen_random_name()","password":"RtjTNDnQH+6uTgFMGo+mSEXEoG++qLtlNcd5Asc5RYs=",
                        "checkPwd":"RtjTNDnQH+6uTgFMGo+mSEXEoG++qLtlNcd5Asc5RYs=","a_id":"1445",
                        "dtype":2,"phone":"15985645499","deadline":"2025-12-30"})
            # .with_data({"atype": 2, "name": "${gen_random_name()}", "sort": "${gen_random_sort()}"})
            .extract()
            .with_jmespath("request.headers", "dls_headers")
            .with_jmespath("request.body", "dls_body")
            .validate()
            .assert_equal("status_code", 200)
        )
    ]
